package com.javasea.qrprint.util;

import java.awt.*;
import java.net.URL;

public class TrayOperatorUtil {
	private static TrayIcon trayIcon;

	public static void hideToSystemTray() {
		//Check the SystemTray is supported
	    if (!SystemTray.isSupported()) {
	        return;
	    }
	    final PopupMenu popup = new PopupMenu();

		URL resource = TrayOperatorUtil.class.getResource("/img/tray_logo.png");
		Image image = Toolkit.getDefaultToolkit().getImage(resource);


		trayIcon = new TrayIcon(image, "客户简介二维码打印");


	    final SystemTray tray = SystemTray.getSystemTray();

	    // Create a pop-up menu components
	    MenuItem exitItem = new MenuItem("Exit"); // 中文乱码
		exitItem.addActionListener(action -> {
			System.exit(0);
		});

	    //Add components to pop-up menu
	    popup.add(exitItem);

	    trayIcon.setPopupMenu(popup);

	    try {
	        tray.add(trayIcon);
	    } catch (AWTException e) {
	    	e.printStackTrace();
	    }
	}

	public static void trayMessage(String message) {
		if (trayIcon != null) {
			trayIcon.displayMessage("消息", message, TrayIcon.MessageType.INFO);
		}
	}
}
